<template>
	<view>
		<!-- 使用自定义的搜索组件 -->
		<view class="search-box">
			<my-search @click="gotoSearch"></my-search>
		</view>
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<!-- 循环渲染轮播图的item项 -->
			<!-- <swiper-item v-for="(item,i) in swiperList" :key="i"> -->
			<!-- <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id"> -->
			<!-- <image :src="item.image_src"></image> -->
			<!-- </navigator> -->
			<!-- </swiper-item> -->
			<swiper-item v-for="item in recommandGoods" :key="item.id">
				<image :src="`http://127.0.0.1:3000/`+item.goodsImage"></image>
			</swiper-item>

		</swiper>

		<!-- 分类导航区域 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item,i) in navList" :key="i" @click="navClickHandler(item)">
				<image :src="item.image_src" class="nav-img"></image>
			</view>
		</view>

		<!-- 推荐盒子 -->
		<view class="wrapper-box">
			<view class="every-box" v-for="item in recommandGoods" :key="item.id" @click='goDetailPage(item)'>
				<image class="image-goods" :src="`http://127.0.0.1:3000/`+item.goodsImage" mode="aspectFit"></image>
				<text>{{item.goodsName}}</text>
			</view>
		</view>

		<!--
		楼层区域
		<view class="floor-list">
			<view class="floor-item" v-for="(item,i) in floorList" :key="i">
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				楼层图片区域
				<view class="floor-img-box">
					左侧大图片的盒子
					<navigator class="left-img-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</navigator>
					右侧4个小图片的盒子
					<view class="right-img-box">
						<navigator class="right-img-item" v-for="(item2,i2) in item.product_list" :key="i2"
							v-if="i2!==0" :url="item2.url">
							<image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width + 'rpx'}">
							</image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
-->
	</view>
</template>

<script>
	// 导入自己封装的mixin模块
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 推荐的数据
				recommandGoods: [],
				// 轮播图的数据
				swiperList: [],
				// 分类导航的数据
				navList: [],
				// 楼层的数据列表
				floorList: [],
			};
		},

		onLoad() {
			// 获取轮播图数据
			// this.getSwiperList()
			// 获取导航分类数据
			this.getNavList()
			// 获取楼层区域数据
			this.getFloorList()
			//获取推荐的商品信息
			this.getRecommandGoods()
		},

		methods: {
			// 点击商品盒子跳转商品详情页面
			goDetailPage(item) {
				console.log(item);
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?id=' + item.id
				})
			},

			// 获取推荐的商品信息
			getRecommandGoods() {
				// 获取商品列表
				uni.request({
					url: 'http://127.0.0.1:3000/goods/getrecommand',
					method: 'GET',
					timeout: 5000,
					success: (res) => {
						res = res.data
						if (res.status == 200) {
							this.recommandGoods = res.data
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error',
							})
						}
					},
					fail: (res) => {
						uni.showToast({
							title: '获取数据失败',
							icon: 'error',
						})
					}
				})
			},

			// 发送轮播图数据请求
			// async getSwiperList() {
			// 	const {
			// 		data: res
			// 	} = await uni.$http.get('/api/public/v1/home/swiperdata')
			// 	if (res.meta.status !== 200) {
			// 		return uni.$showMsg()
			// 	}
			// 	this.swiperList = res.message
			// },

			// 发送分类导航的数据请求 
			async getNavList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.navList = res.message
			},

			// 定义获取楼层列表数据的方法
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) return this.$showMsg()
				res.message.forEach(floor => {
					floor.product_list.forEach(prod => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
			},


			// 点击导航为分类的时候跳转tabbar导航栏的cate
			navClickHandler(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},

			// 点击search组件跳转到search页面
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}

		}
	}
</script>

<style lang="scss">
	.wrapper-box {
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-wrap: wrap;
	}

	.every-box {
		width: 40%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		border: 1px solid yellow;
		margin: 5px;

		.image-goods {
			width: 100%;
		}
	}

	.search-box {
		// 设置定位效果为'吸顶'
		position: sticky;
		// 吸顶的'位置'
		top: 0;
		// 提高层级,防止被轮播图覆盖
		z-index: 999;
	}

	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 30rpx 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-title {
		height: 60rpx;
		width: 100%;
		display: flex;
	}

	.floor-img-box {
		display: flex;
		padding-left: 10rpx;

		.right-img-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
		}
	}
</style>
